﻿//-----------------------------------------------------------------------
// <copyright file="BusinessRangeRule.cs" company="FastNET Services">
//     Copyright (c) 2007-2013. All Rights Reserved.
// </copyright>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings

using System;
using System.Reflection;

#endregion

namespace FastNet.BusinessSystem.Rules
{
    /// <summary>
    /// Class used for managing business rules for an object
    /// </summary>
    public sealed class BusinessRangeRule<TRange> : BusinessRule where TRange : IComparable<TRange>
    {
        #region Variables

        private readonly Func<TRange> _validateMethod;

        #endregion

        #region CTor/DTor

        /// <summary>
        /// Initializes a new instance of the BusinessRangeRule class
        /// </summary>
        /// <param name="ruleName">Name of the rule</param>
        /// <param name="propertyName">Name of the property the rule validates</param>
        /// <param name="errorMessage">Message to display when in error</param>
        /// <param name="validateMethod">Method/Lambda to call when validating rule</param>
        /// <param name="minValue">Minimum acceptable value for this rule</param>
        /// <param name="maxValue">Maximum acceptable value for this rule</param>
        public BusinessRangeRule(string ruleName, string propertyName, string errorMessage, Func<TRange> validateMethod, TRange minValue, TRange maxValue)
            : base(ruleName, propertyName, errorMessage)
        {
            _validateMethod = validateMethod;
            MaximumValue = maxValue;
            MinimumValue = minValue;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets a value referring to the maximum possible for this rule
        /// </summary>
        public TRange MaximumValue { get; private set; }

        /// <summary>
        /// Gets a value referring to the minimum possible for this rule
        /// </summary>
        public TRange MinimumValue { get; private set; }

        #endregion

        #region Methods

        /// <summary>
        /// Called to check the validity of the rule
        /// </summary>
        /// <returns>Boolean whether or not the rule is valid</returns>
        public override bool IsValid()
        {
            TRange value = _validateMethod();
            return (MinimumValue.CompareTo(value) <= 0 && MaximumValue.CompareTo(value) >= 0);
        }

        #endregion
    }
}
